package com.x.epspace.interceptor;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import com.x.epspace.common.AppConfig;
import com.x.epspace.common.DateTools;
import com.x.epspace.dao.OperLogDao;
import com.x.epspace.entity.BindEnt;
import com.x.epspace.entity.OperLog;
import com.x.epspace.entity.RegUser;
import com.x.epspace.ext.HandleSessionService;

//@Component
//@Aspect
public class LogInterceptor {
		
	//@Resource
	private OperLogDao operLogDao;
	
	//@Resource
	private HandleSessionService handleSession;

	//@Pointcut("execution(public * com.x.epspace.service.*.*(..))")
    public void applogic() {}

	//@Around(value = "applogic()") 
    public Object interceptorApplogic(ProceedingJoinPoint pjp) throws Throwable {  
		
        Object regUserObj = handleSession.getVal(AppConfig.getProperty("USER_SESSION_KEY","SESSION_KEY"));
        Object bindEntObj = handleSession.getVal(AppConfig.getProperty("ENT_SESSION_KEY","SESSION_KEY"));
        Object remoteIpObj = handleSession.getVal("remoteIp");
        RegUser regUser = null;
        BindEnt bindEnt = null;
        String remoteIp = "";
        if(regUserObj != null && bindEntObj != null){
			//转换注册用户对象
			regUser = (RegUser)regUserObj;
			//转换绑定企业
			bindEnt = (BindEnt)bindEntObj;
		}
		if(remoteIpObj != null){
			remoteIp = remoteIpObj.toString();
		}
		//调用的方法名
        String method = pjp.getSignature().getName();
        //调用方法的参数
        Object[] args = pjp.getArgs();
        // 获取目标对象
        Object target = pjp.getTarget();
        //获取目标对象的类名(形如：com.action.admin.LoginAction)
        String targetName = pjp.getTarget().getClass().getName();
      //result的值就是被拦截方法的返回值
        Object result = pjp.proceed();
        //构建content
        Map<String,Object> contentMap = new HashMap<String,Object>();
        contentMap.put("targetName", targetName);
        contentMap.put("target", target.toString());
        contentMap.put("method", method);
        contentMap.put("args", args);
        contentMap.put("result", result);
		//转为json串
//        JSONObject contentJson = null;
//		try {
//			contentJson = JSONObject.fromObject(contentMap);
//		} catch (Exception e1) {
//			//e1.printStackTrace();
//		}
        
        //构建日志记录
        OperLog operLog = new OperLog();
		operLog.setRegUser(regUser);
		operLog.setBindEnt(bindEnt);
		operLog.setOperCode(method);
		operLog.setRemoteIp(remoteIp);
		operLog.setOperDate(DateTools.getDisplayTime());
		try {
			operLog.setOperContent(contentMap);
		} catch (Exception e1) { 
			//e1.printStackTrace();
		}
		operLog.setOperTime(System.currentTimeMillis());
		
		//写入日志
		try {
			operLogDao.save(operLog);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return result;
        
    }  
	
	
}
